/**
 * 研发项目分支管理服务
 */

const cloudHelper = require('../config/project_helpers_config').cloudHelper;
const dataHelper = require('../config/path_resolver.js').pageHelper;

class ResearchBranchService {
    /**
     * 获取项目分支列表
     * @param {String} projectId 项目ID
     * @returns {Promise<Array>} 分支列表
     */
    async getBranchList(projectId) {
        if (!projectId) return [];
        
        try {
            const params = {
                projectId
            };
            const result = await cloudHelper.callCloudData('research_branch/list', params);
            return result.list || [];
        } catch (err) {
            console.error('ResearchBranchService.getBranchList 失败:', err);
            return [];
        }
    }

    /**
     * 检查项目是否为分支
     * @param {String} projectId 项目ID
     * @returns {Promise<Boolean>} 是否为分支
     */
    async isBranch(projectId) {
        if (!projectId) return false;
        
        try {
            const params = {
                projectId
            };
            const result = await cloudHelper.callCloudData('research_branch/check', params);
            return result.isBranch || false;
        } catch (err) {
            console.error('ResearchBranchService.isBranch 失败:', err);
            return false;
        }
    }

    /**
     * 获取父项目信息
     * @param {String} projectId 当前项目ID
     * @returns {Promise<Object>} 父项目信息
     */
    async getParentProject(projectId) {
        if (!projectId) return null;
        
        try {
            const params = {
                projectId
            };
            const result = await cloudHelper.callCloudData('research_branch/parent', params);
            return result.parent || null;
        } catch (err) {
            console.error('ResearchBranchService.getParentProject 失败:', err);
            return null;
        }
    }

    /**
     * 创建项目分支
     * @param {String} parentId 父项目ID
     * @param {String} name 分支名称
     * @param {String} desc 分支描述
     * @returns {Promise<Object>} 创建结果
     */
    async createBranch(parentId, name, desc = '') {
        if (!parentId || !name) return null;
        
        try {
            const params = {
                parentId,
                name,
                desc
            };
            return await cloudHelper.callCloudData('research_branch/create', params);
        } catch (err) {
            console.error('ResearchBranchService.createBranch 失败:', err);
            throw err;
        }
    }

    /**
     * 合并项目分支
     * @param {String} sourceId 源分支ID
     * @param {String} targetId 目标项目ID
     * @returns {Promise<Boolean>} 合并结果
     */
    async mergeBranch(sourceId, targetId) {
        if (!sourceId || !targetId) return false;
        
        try {
            const params = {
                sourceId,
                targetId
            };
            const result = await cloudHelper.callCloudData('research_branch/merge', params);
            return !!result.success;
        } catch (err) {
            console.error('ResearchBranchService.mergeBranch 失败:', err);
            throw err;
        }
    }

    /**
     * 更新项目分支
     * @param {String} branchId 分支项目ID
     * @param {String} name 分支名称
     * @param {String} desc 分支描述
     * @returns {Promise<Boolean>} 更新结果
     */
    async updateBranch(branchId, name, desc = '') {
        if (!branchId || !name) return false;
        
        try {
            const params = {
                branchId,
                name,
                desc
            };
            const result = await cloudHelper.callCloudData('research_branch/update', params);
            return !!result.success;
        } catch (err) {
            console.error('ResearchBranchService.updateBranch 失败:', err);
            throw err;
        }
    }

    /**
     * 删除项目分支
     * @param {String} branchId 分支项目ID
     * @returns {Promise<Boolean>} 删除结果
     */
    async deleteBranch(branchId) {
        if (!branchId) return false;
        
        try {
            const params = {
                branchId
            };
            const result = await cloudHelper.callCloudData('research_branch/delete', params);
            return !!result.success;
        } catch (err) {
            console.error('ResearchBranchService.deleteBranch 失败:', err);
            throw err;
        }
    }

    /**
     * 获取分支版本历史
     * @param {String} branchId 分支ID
     * @returns {Promise<Array>} 版本历史
     */
    async getBranchVersions(branchId) {
        if (!branchId) return [];
        
        try {
            const params = {
                branchId
            };
            const result = await cloudHelper.callCloudData('research_branch/versions', params);
            return result.list || [];
        } catch (err) {
            console.error('ResearchBranchService.getBranchVersions 失败:', err);
            return [];
        }
    }

    /**
     * 切换到指定版本
     * @param {String} branchId 分支ID
     * @param {String} versionId 版本ID
     * @returns {Promise<Boolean>} 切换结果
     */
    async switchToVersion(branchId, versionId) {
        if (!branchId || !versionId) return false;
        
        try {
            const params = {
                branchId,
                versionId
            };
            const result = await cloudHelper.callCloudData('research_branch/switch_version', params);
            return !!result.success;
        } catch (err) {
            console.error('ResearchBranchService.switchToVersion 失败:', err);
            throw err;
        }
    }
}

module.exports = ResearchBranchService; 